
var getData = function () {
    return {
        docList: [
            { id: '35', folderId: 'root', title: '35', status: 'orange', version: '001', },
            { id: '32', folderId: 'root', title: '32', status: 'blue', version: '001', },
            { id: '34', folderId: 'root', title: '34', status: 'red', version: '001', },
            { id: '31', folderId: 'root', title: '31', status: 'green', version: '001', },
            { id: '33', folderId: 'root', title: '33', status: 'gray', version: '001', },
            { id: 'aaa1', folderId: 'aaa', title: 'aaa1', status: 'orange', version: '001', },
            { id: 'aaa2', folderId: 'aaa', title: 'aaa2', status: 'blue', version: '001', },
            { id: 'aaa3', folderId: 'aaa', title: 'aaa3', status: 'red', version: '001', },
            { id: 'aaa4', folderId: 'aaa', title: 'aaa4', status: 'green', version: '001', },
            { id: 'c1', folderId: 'bbb', title: 'c1', status: 'orange', version: '001', },
            { id: 'c2', folderId: 'bbb', title: 'c2', status: 'blue', version: '001', },
            { id: 'c3', folderId: 'bbb', title: 'c3', status: 'red', version: '001', },
            { id: 'c4', folderId: 'bbb', title: 'c4', status: 'green', version: '001', },
            { id: 'c11', folderId: 'bbb', title: 'c11', status: 'orange', version: '001', },
            { id: 'c12', folderId: 'bbb', title: 'c12', status: 'blue', version: '001', },
            { id: 'c13', folderId: 'bbb', title: 'c13', status: 'red', version: '001', },
            { id: 'c14', folderId: 'bbb', title: 'c14', status: 'green', version: '001', },
            { id: 'c21', folderId: 'bbb', title: 'c21', status: 'orange', version: '001', },
            { id: 'c22', folderId: 'bbb', title: 'c22', status: 'blue', version: '001', },
            { id: 'c23', folderId: 'bbb', title: 'c23', status: 'red', version: '001', },
            { id: 'c24', folderId: 'bbb', title: 'c24', status: 'green', version: '001', },
            { id: 'c31', folderId: 'bbb', title: 'c31', status: 'orange', version: '001', },
            { id: 'c32', folderId: 'bbb', title: 'c32', status: 'blue', version: '001', },
            { id: 'c33', folderId: 'bbb', title: 'c33', status: 'red', version: '001', },
            { id: 'c34', folderId: 'bbb', title: 'c34', status: 'green', version: '001', },
            { id: 'c41', folderId: 'bbb', title: 'c41', status: 'orange', version: '001', },
            { id: 'c42', folderId: 'bbb', title: 'c42', status: 'blue', version: '001', },
            { id: 'c43', folderId: 'bbb', title: 'c43', status: 'red', version: '001', },
            { id: 'c44', folderId: 'bbb', title: 'c44', status: 'green', version: '001', },
        ],
        folderList: [
            { id: 'root', folderParentId: null, title: '根目录', },
            { id: 'aaa', folderParentId: 'root', title: 'aaa', },
            { id: 'bbb', folderParentId: 'root', title: 'bbb', },
            { id: 'b1', folderParentId: 'aaa', title: 'b1', status: 'orange', version: '001', },
            { id: 'b2', folderParentId: 'aaa', title: 'b2', status: 'blue', version: '001', },
            { id: 'b3', folderParentId: 'aaa', title: 'b3', status: 'red', version: '001', },
            { id: 'b4', folderParentId: 'aaa', title: 'b4', status: 'green', version: '001', },
            { id: 'd1', folderParentId: 'bbb', title: 'd1', status: 'orange', version: '001', },
            { id: 'd2', folderParentId: 'bbb', title: 'd2', status: 'blue', version: '001', },
            { id: 'd3', folderParentId: 'bbb', title: 'd3', status: 'red', version: '001', },
            { id: 'd4', folderParentId: 'bbb', title: 'd4', status: 'green', version: '001', },
        ],
    };
};


const state = {
    DOC_ACTIVE_ID: 'aaa',
    tree: {
        id: 'root',
        title: '根目录',
        docList: [],
        folderList: [],
    },
    treeRef: null,
    closeAllFolder: 0,
    shellMenuSetting: {
        status: false,
        option: { X: 0, Y: 0 },
        object: {},
    },
    docObject: {},
    folderObject: {},
    markSetting: {
        status: false, // 移动状态
        type: null, // 移动类型
        moveObject: {}, // 移动对象
        moveType: null, // 移动到类型
        moveValue: {}, // 移动结果
        moveValueType: '', // 移动类型
        setting: {  // 移动位置
            x: -100,
            y: -100,
            title: '',
        },
    },
    folderEidtor: {
        object: {},
        status: false,
        type: null, // new update
    },
};
const getters = {};
const mutations = {};
const actions = {
    // 关闭全部文件夹
    closeAllFolder(context) {
        context.state.closeAllFolder++;
    },
    // 绑定形状树元素
    setTreeRef(context, tree) {
        context.state.treeRef = tree;
    },
    // 
    getRenderData(context) {
        // action=> 获取所有API数据
        window.action.doc.getRenderData().then((res) => {
            if (res) {
                context.dispatch('renderTree', res);
            };
        });
    },
    getTreeData(context, callback) {
        callback(JSON.parse(JSON.stringify(context.state.tree)));
    },
    // 生成树形结构
    renderTree(context, obj) {
        // let obj = {};
        // let obj = getData();
        let rootFolder = {};
        var docObject = {};
        var folderObject = {};

        for (let i = 0; i < obj.docList.length; i++) {
            const element = obj.docList[i];
            docObject[element.id] = element;
        };

        for (let i = 0; i < obj.folderList.length; i++) {
            const folder = obj.folderList[i];
            folderObject[folder.id] = folder;
            folder.child_folder = {};
            folder.child_doc = {};
        };

        for (let i = 0; i < obj.folderList.length; i++) {
            const folder = obj.folderList[i];
            if (folder.folderParentId) {
                folderObject[folder.folderParentId].child_folder[folder.id] = folder;
            } else {
                rootFolder = folder;
            }
        };

        for (let i = 0; i < obj.docList.length; i++) {
            const doc = obj.docList[i];
            folderObject[doc.folderId].child_doc[doc.id] = doc;
        };

        for (const key in folderObject) {
            if (folderObject.hasOwnProperty(key)) {
                const folder = folderObject[key];
                if (!folder.folderList) {
                    folder.folderList = [];
                };
                if (!folder.docList) {
                    folder.docList = [];
                };
                if (folder.folderIndexArray) {
                    let folderIndexArray = folder.folderIndexArray.split(';');
                    for (let i = 0; i < folderIndexArray.length; i++) {
                        const childFolderId = folderIndexArray[i];
                        if (folder.child_folder.hasOwnProperty(childFolderId)) {
                            let childFolder = folder.child_folder[childFolderId];
                            folder.folderList.push(childFolder);
                            delete folder.child_folder[childFolderId];
                        };
                    }
                };
                for (const key in folder.child_folder) {
                    if (folder.child_folder.hasOwnProperty(key)) {
                        const childFolder = folder.child_folder[key];
                        folder.folderList.push(childFolder);
                    }
                };
                if (folder.childIndexArray) {
                    let childIndexArray = folder.childIndexArray.split(';');
                    for (let i = 0; i < childIndexArray.length; i++) {
                        const childApiId = childIndexArray[i];
                        if (folder.child_doc.hasOwnProperty(childApiId)) {
                            let childApi = folder.child_doc[childApiId];
                            folder.docList.push(childApi);
                            delete folder.child_doc[childApiId];
                        };
                    }
                };
                for (const key in folder.child_doc) {
                    if (folder.child_doc.hasOwnProperty(key)) {
                        const childApi = folder.child_doc[key];
                        folder.docList.push(childApi);
                    }
                };
            }
        };

        for (let i = 0; i < obj.folderList.length; i++) {
            const folder = obj.folderList[i];
            // folderObject[folder.id] = folder;
            delete folder.child_folder;
            delete folder.child_doc;
        };
        context.state.tree = rootFolder;
        context.state.docObject = docObject;
        context.state.folderObject = folderObject;
    },
    // 更改拖拽位置
    setMoveSetting(context, obj) {
        context.state.markSetting.setting.x = obj.x;// 移动位置
        context.state.markSetting.setting.y = obj.y;// 移动位置
    },
    // 更改拖拽对象
    setMoveValue(context, obj) {
        context.state.markSetting.moveType = obj.type;
        context.state.markSetting.moveValue = obj.value;
        context.state.markSetting.moveValueType = obj.moveValueType;
    },
    // 初始化拖拽对象
    setMoveStatus(context, obj) {
        context.state.markSetting.status = true;
        context.state.markSetting.type = obj.type;
        context.state.markSetting.moveObject = obj.moveObject;
        context.state.markSetting.setting.title = obj.moveObject.title;
    },
    // 更改焦点对象
    setActiveId(context, DOC_ACTIVE_ID) {
        context.state.DOC_ACTIVE_ID = DOC_ACTIVE_ID;
    },
    // 移除拖拽对象
    setMoveOut(context) {
        context.state.markSetting.moveType = null;
        context.state.markSetting.moveValue = {};
    },
    // 清除移动位置
    clearMoveMark(context) {
        context.state.markSetting.setting = {  // 移动位置
            x: -100,
            y: -100,
            title: '',
        };
    },
    // 清除移动状态
    clearMoveStatus(context) {
        context.state.markSetting.moveType = false;
        context.state.markSetting.status = false;
        context.state.markSetting.type = null;
        context.state.markSetting.moveObject = {};
        context.state.markSetting.moveValue = {};
        // context.dispatch('clearMoveMark');
    },
    // 验证移动，调用更改方法
    checkMove(context) {
        // 没有移动类型，不进行移动
        if (context.state.markSetting.moveType === null) {
            context.dispatch('clearMoveStatus');
            return;
        };
        var move_type = context.state.markSetting.type;
        var move_value_type = context.state.markSetting.moveValueType;
        var move_to_type = context.state.markSetting.moveType;

        var move_object = JSON.parse(JSON.stringify(context.state.markSetting.moveObject));
        var _$move_object = context.state.markSetting.moveObject;

        var move_to_object = JSON.parse(JSON.stringify(context.state.markSetting.moveValue));
        var _$move_to_object = context.state.markSetting.moveValue

        if (move_type == 'doc') {
            var old_folder = JSON.parse(JSON.stringify(context.state.folderObject[move_object.folderId]));
            var _$old_folder = context.state.folderObject[move_object.folderId];
        } else if (move_type == 'folder') {
            var old_folder = JSON.parse(JSON.stringify(context.state.folderObject[move_object.folderParentId]));
            var _$old_folder = context.state.folderObject[move_object.folderParentId];
        };

        if (move_value_type == 'doc') {
            var new_folder = JSON.parse(JSON.stringify(context.state.folderObject[move_to_object.folderId]));
            var _$new_folder = context.state.folderObject[move_to_object.folderId];
        } else if (move_value_type == 'folder' && move_to_type != 'intoFolder') {
            var new_folder = JSON.parse(JSON.stringify(context.state.folderObject[move_to_object.folderParentId]));
            var _$new_folder = context.state.folderObject[move_to_object.folderParentId];
        } else if (move_value_type == 'folder' && move_to_type == 'intoFolder') {
            var new_folder = move_to_object;
            var _$new_folder = _$move_to_object;
        };
        if (old_folder.id == new_folder.id && move_to_object.id == move_object.id) {
            // 没有进行移动
            context.dispatch('clearMoveStatus');
            return;
        };
        if (old_folder.id == new_folder.id && move_value_type == move_type) {
            var childOrderArray = [];
            var oldIndex = null;
            var newIndex = null;
            for (let i = 0; i < new_folder[move_value_type + 'List'].length; i++) {
                const element = new_folder[move_value_type + 'List'][i];
                if (element.id == move_object.id) {
                    oldIndex = i;
                };
                childOrderArray.push(element.id);
            };
            childOrderArray.splice(oldIndex, 1);
            for (let i = 0; i < childOrderArray.length; i++) {
                const element = childOrderArray[i];
                if (element == move_to_object.id) {
                    newIndex = i;
                };
            };
            if (move_to_type == 'TopMoveMark') {
                childOrderArray.splice(newIndex, 0, move_object.id)
            } else if (move_to_type == 'BottomMoveMark') {
                childOrderArray.splice(newIndex + 1, 0, move_object.id)
            } else if (move_to_type == 'intoFolder') {
                childOrderArray.unshift(move_object.id)
            };
            context.dispatch('clearMoveStatus');

            window.action.doc.folder.move('same', move_value_type, new_folder, childOrderArray).then((res) => {
                if (res) {
                    // ajax 执行后运行
                    _$new_folder[move_value_type + 'List'].splice(oldIndex, 1);
                    if (move_to_type == 'TopMoveMark') {
                        _$new_folder[move_value_type + 'List'].splice(newIndex, 0, _$move_object)
                    } else if (move_to_type == 'BottomMoveMark') {
                        _$new_folder[move_value_type + 'List'].splice(newIndex + 1, 0, _$move_object)
                    } else if (move_to_type == 'intoFolder') {
                        _$new_folder[move_value_type + 'List'].unshift(_$move_object)
                    };
                };
            });
        } else if (old_folder.id != new_folder.id) {
            var _data = {
                id: move_object.id,
                type: move_type,
                from: {
                    id: old_folder.id,
                    childArray: [],
                },
                to: {
                    id: new_folder.id,
                    childArray: [],
                },
            };
            var oldIndex = null;
            for (let i = 0; i < old_folder[move_type + 'List'].length; i++) {
                const element = old_folder[move_type + 'List'][i];
                if (element.id == move_object.id) {
                    oldIndex = i;
                };
                _data.from.childArray.push(element.id);
            };
            _data.from.childArray.splice(oldIndex, 1);
            _data.from[move_type + 'List']
            var newIndex = null;
            for (let i = 0; i < new_folder[move_type + 'List'].length; i++) {
                const element = new_folder[move_type + 'List'][i];
                if (element.id == move_to_object.id) {
                    newIndex = i;
                };
                _data.to.childArray.push(element.id);
            };
            if (move_to_type == 'TopMoveMark') {
                _data.to.childArray.splice(newIndex, 0, move_object.id)
            } else if (move_to_type == 'BottomMoveMark') {
                _data.to.childArray.splice(newIndex + 1, 0, move_object.id)
            } else if (move_to_type == 'intoFolder') {
                _data.to.childArray.unshift(move_object.id)
            };
            context.dispatch('clearMoveStatus');
            // ajax 执行后运行
            window.action.doc.folder.move('different', move_value_type, _data).then((res) => {
                if (res) {
                    _$old_folder[move_type + 'List'].splice(oldIndex, 1);
                    if (move_type == 'doc') {
                        _$move_object.folderId = _$new_folder.id;
                    } else if (move_type == 'folder') {
                        _$move_object.folderParentId = _$new_folder.id;
                    };
                    if (move_to_type == 'TopMoveMark') {
                        _$new_folder[move_type + 'List'].splice(newIndex, 0, _$move_object)
                    } else if (move_to_type == 'BottomMoveMark') {
                        _$new_folder[move_type + 'List'].splice(newIndex + 1, 0, _$move_object)
                    } else if (move_to_type == 'intoFolder') {
                        _$new_folder[move_type + 'List'].unshift(_$move_object)
                    };
                };
            });


        } if (old_folder.id == new_folder.id && move_value_type != move_type) {
            var childOrderArray = [];
            var oldIndex = null;
            for (let i = 0; i < new_folder[move_type + 'List'].length; i++) {
                const element = new_folder[move_type + 'List'][i];
                if (element.id == move_object.id) {
                    oldIndex = i;
                };
                childOrderArray.push(element.id);
            };
            childOrderArray.splice(oldIndex, 1);
            if (move_to_type == 'intoFolder') {
                childOrderArray.unshift(move_object.id)
            };
            context.dispatch('clearMoveStatus');
            window.action.doc.folder.move('same', move_value_type, new_folder, childOrderArray).then((res) => {
                if (res) {
                    context.dispatch('clearMoveStatus');
                    // ajax 执行后运行
                    _$new_folder[move_type + 'List'].splice(oldIndex, 1);
                    if (move_to_type == 'intoFolder') {
                        _$new_folder[move_type + 'List'].unshift(_$move_object)
                    };
                };
            });
        } else {
            context.dispatch('clearMoveStatus');
        };
    },
    // 关闭右键菜单
    closeShellMenu(context) {
        context.state.shellMenuSetting = {
            status: false,
            type: null,
            option: { X: 0, Y: 0 },
            object: {},
        };
    },
    // 展开右键菜单
    showShellMenu(context, obj) {
        context.state.shellMenuSetting = {
            status: true,
            type: obj.type,
            option: { X: obj.option.X, Y: obj.option.Y },
            object: obj.object,
        };
    },
    // 开启文件夹编辑
    setFolderEdit(context, obj) {
        context.state.folderEidtor = {
            object: obj.value,
            status: true,
            type: obj.type, // new update
        };
    },
    // 关闭文件夹编辑
    clearFolderEidt(context) {
        context.state.folderEidtor = {
            object: {},
            status: false,
            type: null, // new update
        };
    },
    // 编辑文件夹
    editFolder(context, obj) {
        var title = obj.title;
        var description = obj.description;
        if (context.state.folderEidtor.type == 'newFolder') {
            var folderId = context.state.folderEidtor.object.id;
            var parentFolder = context.state.folderObject[folderId];
            window.action.doc.folder.creat({
                title: title,
                description: description,
            }, parentFolder).then((id) => {
                if (id) {
                    var parentFolder = context.state.folderObject[folderId];
                    var folderObject = {
                        id: id,
                        title: title,
                        folderId: id,
                        folderParentId: parentFolder.folderId,
                        description: description,
                        folderIndexArray: null,
                        childIndexArray: null,
                        docList: [],
                        folderList: [],
                    };
                    parentFolder.folderList.unshift(folderObject);
                    context.state.folderObject[id] = folderObject;
                };
            });
            context.dispatch('clearFolderEidt');
        } else if (context.state.folderEidtor.type == 'updateFolder') {
            var folderId = context.state.folderEidtor.object.id;
            var parentFolder = context.state.folderObject[folderId];
            window.action.doc.folder.updata({
                title: title,
                description: description,
            }, parentFolder).then((res) => {
                if (res) {
                    parentFolder.title = title;
                    parentFolder.description = description;
                    context.dispatch('clearFolderEidt');
                };
            });
        };
    },
    // 删除文件夹
    deleteFolder(context) {
        var folderId = context.state.shellMenuSetting.object.id;
        var ParentId = context.state.shellMenuSetting.object.folderParentId;
        var Folder = context.state.folderObject[folderId];
        var Parent = context.state.folderObject[ParentId];
        window.action.doc.folder.delete(Folder, Parent).then((res) => {
            if (res) {
                for (let i = 0; i < Parent.folderList.length; i++) {
                    const element = Parent.folderList[i];
                    if (element == Folder) {
                        Parent.folderList.splice(i, 1);
                        break;
                    };
                };
                for (let i = 0; i < Folder.folderList.length; i++) {
                    const element = Folder.folderList[i];
                    element.folderParentId = Parent.id;
                    Parent.folderList.push(element);
                };
                for (let i = 0; i < Folder.docList.length; i++) {
                    const element = Folder.docList[i];
                    element.folderId = Parent.id;
                    Parent.docList.push(element);
                };
            };
        });
    },
    addDoc(context, doc) {
        context.state.docObject[doc.id] = doc;
        context.state.folderObject[doc.folderId].docList.push(doc);        
    },
    // 删除文档
    deleteDoc(context, obj) {
        
    },
    // 另存为
    saveAs(context, obj) { },
};

export default {
    namespaced: true,
    state,
    getters,
    mutations,
    actions
}